package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.minio.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.WmUserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private MinIoTemplate minIoTemplate;


    @Override
    public ResponseResult upload(MultipartFile file) throws IOException {

        //将文件变成输入流  springboot中直接getInputStream
        InputStream inputStream = file.getInputStream();
        //获取文件属性
        String contentType = file.getContentType();
        if(StringUtils.isEmpty(contentType)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        String[] split = contentType.split("/");
        String fileName = split[1];
        //上传到minio
        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() +"."+ fileName, inputStream, "image");
        //创建个对象
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUserId(WmUserThreadLocal.get().getId());

        //存入mysql
        int insertResult = wmMaterialMapper.insert(wmMaterial);
        if(insertResult < 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //返回
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult findList(WmMaterialDto dto) {
        if(dto == null){
            return new PageResponseResult(0,0,0);
        }
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection() == 1,WmMaterial::getIsCollection,dto.getIsCollection());

        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        Page<WmMaterial> pageResult = wmMaterialMapper.selectPage(page, wrapper);

        Integer total = Math.toIntExact(pageResult.getTotal());

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), total);
        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }
}
